package com.rwhansen.humidor;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;

//import test.TableDate.DateRenderer;

public class PersonalCigarAssistant {

	private static JFrame frmPersonalCigarAssistant;
	static JTable ratingsTable;
	static JTable cigarDatabaseTable;
	static JTable humidorTable;
	private Cigar cigar;
	private static JPanel mainPanel;
	private static CigarDetailHumidorPanel cigarDetailHumidorPanel;
	private static CigarDetailRatingPanel cigarDetailRatingPanel;
	private CigarDetailDatabasePanel cigarDetailDatabasePanel;
	public static Database cigarDatabase;
	public static Database humidorDatabase;
	public static Database ratingsDatabase;
	static JScrollPane humidorScrollPane;
	static JScrollPane cigarDatabaseScrollPane;
	static JScrollPane ratingsScrollPane;
	static JTabbedPane tabbedPane;
	private JMenuBar menuBar;
	private JMenu mnHelp;
	private JMenuItem rdbtnmntmAbout;
	private JMenu mnCigars;
	private JMenuItem rdbtnmntmStats;
	private JMenu mnDatabase;
	private JMenu mnHumidor;
	private JMenu mnRatings;
	private JMenuItem mntmAddCigarDatabase;
	private JMenuItem mntmEditCigarDatabase;
	private JMenuItem mntmRemoveCigarDatabase;
	private JSeparator separatorDatabase;
	private JMenuItem mntmSmokeCigarInDatabase;
	private JMenuItem mntmDatabaseCigarToHumidor;
	private JMenuItem mntmEditCigarHumidor;
	private JMenuItem mntmRemoveCigarHumidor;
	private JSeparator separatorHumidor;
	private JMenuItem mntmEditCigarRatings;
	private JMenuItem mntmRemoveCigarRatings;
	private JMenuItem mntmSmokeCigarHumidor;
	private JMenu mnFile;
	private JMenuItem mntmPrintCigarDetails;
	private JMenuItem mntmQuit;
	private JSeparator separator;
	private JMenuItem mntmPrintDatabase;
	private JMenuItem mntmPrintHumidor;
	private JSeparator separator_1;
	private JSeparator separator_2;
	private JMenuItem mntmPrintRatings;
	private JSeparator separator_3;
	private JMenuItem mntmPickACigar;
	private JSeparator separator_4;
	private JMenuItem mntmMyInfo;
	
	public static final String VERSION = "2.1.113";;
	public static final String LAST_UPDATE = "02/18/2012";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@SuppressWarnings("static-access")
			public void run() {
				try {
					PersonalCigarAssistant window = new PersonalCigarAssistant();
					window.frmPersonalCigarAssistant.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public PersonalCigarAssistant() {
	
		getDatabases();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		//TODO: need to refresh the tabs or the index of the tab will not match the index of the arraylist<cigar>
		cigar = new Cigar("New Cigar");
		cigar.setClearCigar();
		
		frmPersonalCigarAssistant = new JFrame();
		frmPersonalCigarAssistant.setIconImage(Toolkit.getDefaultToolkit().getImage(PersonalCigarAssistant.class.getResource("/com/rwhansen/humidor/favicon.ico")));
		frmPersonalCigarAssistant.setEnabled(true);
		frmPersonalCigarAssistant.setTitle("Personal Cigar Assistant");
		frmPersonalCigarAssistant.setBounds(100, 100, 1399, 849);
		frmPersonalCigarAssistant.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmPersonalCigarAssistant.setFocusable(false);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		frmPersonalCigarAssistant.getContentPane().add(tabbedPane, BorderLayout.WEST);

//Builds Tables		
		cigarDatabaseScrollPane = new JScrollPane();
		tabbedPane.addTab("Cigar Database", null, cigarDatabaseScrollPane, null);
		cigarDatabaseTable = new JTable();
				
		humidorScrollPane = new JScrollPane();
		tabbedPane.addTab("My Humidor", null, humidorScrollPane, null);
		humidorTable = new JTable();
		
		ratingsScrollPane = new JScrollPane();
		tabbedPane.addTab("Cigar Ratings", null, ratingsScrollPane, null);
		ratingsTable = new JTable();
		
		refreshTables();
	
		
//Sets the default tab to humidor and select the fist cigar
		tabbedPane.setSelectedComponent(humidorScrollPane);
//		humidorTable.changeSelection(0, 0, false, false);
		
		if (!humidorDatabase.getCigars().isEmpty()) {
			cigar = humidorDatabase.getCigars().get(humidorTable.convertRowIndexToModel(0));
		}
		
		
				
//Build main detail panel
		mainPanel = new JPanel();
		frmPersonalCigarAssistant.getContentPane().add(mainPanel, BorderLayout.CENTER);
		mainPanel.setLayout(new BorderLayout(0, 0));
		
		cigarDetailRatingPanel = new CigarDetailRatingPanel(cigar, ratingsDatabase);
		cigarDetailDatabasePanel = new CigarDetailDatabasePanel(cigar, cigarDatabase, humidorDatabase, ratingsDatabase);
		cigarDetailHumidorPanel = new CigarDetailHumidorPanel(cigar, humidorDatabase, ratingsDatabase, humidorDatabase.getCigars().indexOf(cigar));
		mainPanel.add(cigarDetailHumidorPanel, BorderLayout.CENTER);
		cigarDetailHumidorPanel.setLayout(null);
			
		CigarDatabaseSelectionListener databaseListener = new CigarDatabaseSelectionListener(cigarDatabaseTable);
		cigarDatabaseTable.getSelectionModel().addListSelectionListener(databaseListener);
		cigarDatabaseTable.getColumnModel().getSelectionModel().addListSelectionListener(databaseListener);
		
		HumidorSelectionListener humidorListener = new HumidorSelectionListener(humidorTable);
		humidorTable.getSelectionModel().addListSelectionListener(humidorListener);
		humidorTable.getColumnModel().getSelectionModel().addListSelectionListener(humidorListener);
		
		RatingsSelectionListener ratingsListener = new RatingsSelectionListener(ratingsTable);
		ratingsTable.getSelectionModel().addListSelectionListener(ratingsListener);
		ratingsTable.getColumnModel().getSelectionModel().addListSelectionListener(ratingsListener);
		
//Tab Listener
		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				refreshTables();
				
				if (tabbedPane.getSelectedIndex() == 0) {
					cigarDatabaseTable.changeSelection(0, 0, false, false);
				} else if (tabbedPane.getSelectedIndex() == 1) {
					humidorTable.changeSelection(0, 0, false, false);
				} else if (tabbedPane.getSelectedIndex() == 2) {
					ratingsTable.changeSelection(0, 0, false, false);
				}
				
			}
		});
	
//Menu Bar
		menuBar = new JMenuBar();
		menuBar.setFont(new Font("GodOfWar", Font.PLAIN, 14));
		menuBar.setBackground(Color.GRAY);
		frmPersonalCigarAssistant.setJMenuBar(menuBar);
		
		mnFile = new JMenu("File");
		mnFile.setBackground(Color.GRAY);
		mnFile.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		menuBar.add(mnFile);
		
		mntmQuit = new JMenuItem("Quit");
		mntmQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		
		mntmMyInfo = new JMenuItem("My Info");
		mntmMyInfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyInfo myInfo = new MyInfo();
				myInfo.frmStats.setVisible(true);
				myInfo.frmStats.requestFocus();
			}
		});
		mntmMyInfo.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mnFile.add(mntmMyInfo);
		
		rdbtnmntmStats = new JMenuItem("Stats");
		mnFile.add(rdbtnmntmStats);
		rdbtnmntmStats.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Stats stats = new Stats(tabbedPane.getSelectedIndex());
				stats.frmStats.setVisible(true);
				stats.frmStats.requestFocus();
			}
		});
		rdbtnmntmStats.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		
		separator_4 = new JSeparator();
		mnFile.add(separator_4);
		mntmQuit.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmQuit.setEnabled(true);
		mnFile.add(mntmQuit);
		
		mnDatabase = new JMenu("Database");
		mnDatabase.setBackground(Color.GRAY);
		mnDatabase.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		menuBar.add(mnDatabase);
		
		mntmAddCigarDatabase = new JMenuItem("Add New Cigar To Database");
		mntmAddCigarDatabase.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				buildCigarDatabaseDetails(cigar);
				CigarDetailDatabasePanel.editFields(true);
				CigarDetailDatabasePanel.setCigarDatabaseButtonsVisible(false, false);
				CigarDetailDatabasePanel.delete = false;
				CigarDetailDatabasePanel.newCigarDataFields();
				CigarDetailDatabasePanel.textName.setText("New Cigar");
				
				
			}
		});
		mntmAddCigarDatabase.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmAddCigarDatabase.setEnabled(true);
		mnDatabase.add(mntmAddCigarDatabase);
		
		mntmEditCigarDatabase = new JMenuItem("Edit Cigar In Database");
		mntmEditCigarDatabase.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmEditCigarDatabase.setEnabled(false);
		mnDatabase.add(mntmEditCigarDatabase);
		
		mntmRemoveCigarDatabase = new JMenuItem("Remove Cigar From Database");
		mntmRemoveCigarDatabase.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmRemoveCigarDatabase.setEnabled(false);
		mnDatabase.add(mntmRemoveCigarDatabase);
		
		separatorDatabase = new JSeparator();
		mnDatabase.add(separatorDatabase);
		
		mntmDatabaseCigarToHumidor = new JMenuItem("Add Cigar From Database To Humidor");
		mntmDatabaseCigarToHumidor.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmDatabaseCigarToHumidor.setEnabled(false);
		mnDatabase.add(mntmDatabaseCigarToHumidor);
		
		mntmSmokeCigarInDatabase = new JMenuItem("Smoke Cigar In Database");
		mntmSmokeCigarInDatabase.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				cigar.setHumidor("Not Kept");
				cigar.setDateAddHumidor("T");
				cigar.setDateSmoked("T");
				
				PersonalCigarAssistant.buildRatingDetails(cigar);
				CigarDetailRatingPanel.editFields(true);
				
			}
		});
		mntmSmokeCigarInDatabase.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmSmokeCigarInDatabase.setEnabled(false);
		mnDatabase.add(mntmSmokeCigarInDatabase);
		
		separator = new JSeparator();
		mnDatabase.add(separator);
		
		mntmPrintDatabase = new JMenuItem("Print Database List");
		mntmPrintDatabase.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmPrintDatabase.setEnabled(false);
		mnDatabase.add(mntmPrintDatabase);
		
		mnHumidor = new JMenu("Humidor");
		mnHumidor.setBackground(Color.GRAY);
		mnHumidor.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		menuBar.add(mnHumidor);
		
		mntmEditCigarHumidor = new JMenuItem("Edit Cigar In Humidor");
		mntmEditCigarHumidor.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmEditCigarHumidor.setEnabled(false);
		mnHumidor.add(mntmEditCigarHumidor);
		
		mntmRemoveCigarHumidor = new JMenuItem("Remove Cigar From Humidor");
		mntmRemoveCigarHumidor.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmRemoveCigarHumidor.setEnabled(false);
		mnHumidor.add(mntmRemoveCigarHumidor);
		
		separatorHumidor = new JSeparator();
		mnHumidor.add(separatorHumidor);
		
		mntmSmokeCigarHumidor = new JMenuItem("Smoke Cigar In Humidor");
		mntmSmokeCigarHumidor.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmSmokeCigarHumidor.setEnabled(false);
		mnHumidor.add(mntmSmokeCigarHumidor);
		
		mntmPickACigar = new JMenuItem("Pick a Cigar For Me");
		mntmPickACigar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				PickACigarForMe pick = new PickACigarForMe();
				pick.frmStats.setVisible(true);
				pick.frmStats.requestFocus();
				
			}
		});
		mntmPickACigar.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mnHumidor.add(mntmPickACigar);
		
		separator_1 = new JSeparator();
		mnHumidor.add(separator_1);
		
		mntmPrintHumidor = new JMenuItem("Print Humidor");
		mntmPrintHumidor.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmPrintHumidor.setEnabled(false);
		mnHumidor.add(mntmPrintHumidor);
		
		mnRatings = new JMenu("Ratings");
		mnRatings.setBackground(Color.GRAY);
		mnRatings.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		menuBar.add(mnRatings);
		
		mntmEditCigarRatings = new JMenuItem("Edit Cigar In Database");
		mntmEditCigarRatings.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmEditCigarRatings.setEnabled(false);
		mnRatings.add(mntmEditCigarRatings);
		
		mntmRemoveCigarRatings = new JMenuItem("Remove Cigar From Database");
		mntmRemoveCigarRatings.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmRemoveCigarRatings.setEnabled(false);
		mnRatings.add(mntmRemoveCigarRatings);
		
		separator_2 = new JSeparator();
		mnRatings.add(separator_2);
		
		mntmPrintRatings = new JMenuItem("Print Ratings");
		mntmPrintRatings.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmPrintRatings.setEnabled(false);
		mnRatings.add(mntmPrintRatings);
		
		mnCigars = new JMenu("Cigars");
		mnCigars.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mnCigars.setBackground(Color.GRAY);
		menuBar.add(mnCigars);
		
		separator_3 = new JSeparator();
		mnCigars.add(separator_3);
		
		mntmPrintCigarDetails = new JMenuItem("Print Cigar Details");
		mnCigars.add(mntmPrintCigarDetails);
		mntmPrintCigarDetails.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mntmPrintCigarDetails.setEnabled(false);
		
		mnHelp = new JMenu("Help");
		mnHelp.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mnHelp.setBackground(Color.GRAY);
		menuBar.add(mnHelp);
		
		rdbtnmntmAbout = new JMenuItem("About");
		rdbtnmntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				About about = new About(LAST_UPDATE, VERSION);
				about.frmStats.setVisible(true);
				about.frmStats.requestFocus();
				
			}
		});
		rdbtnmntmAbout.setFont(new Font("GodOfWar", Font.PLAIN, 12));
		mnHelp.add(rdbtnmntmAbout);
		
	}
	
	public void buildCigarDatabaseDetails(Cigar cigar) {
		mainPanel.setVisible(false);
		mainPanel.removeAll();
		cigarDetailDatabasePanel = new CigarDetailDatabasePanel(cigar, cigarDatabase, humidorDatabase, ratingsDatabase);
		mainPanel.add(cigarDetailDatabasePanel, BorderLayout.CENTER);
		cigarDetailDatabasePanel.setLayout(null);
		mainPanel.repaint();
		mainPanel.setVisible(true);
	}
	
	public static void buildHumidorDetails(Cigar cigar) {
		mainPanel.setVisible(false);
		mainPanel.removeAll();
    	cigarDetailHumidorPanel = new CigarDetailHumidorPanel(cigar, humidorDatabase, ratingsDatabase, humidorDatabase.getCigars().indexOf(cigar));
		mainPanel.add(cigarDetailHumidorPanel, BorderLayout.CENTER);
		cigarDetailHumidorPanel.setLayout(null);
		mainPanel.repaint();
		mainPanel.setVisible(true);
	}
	
	public static void buildRatingDetails(Cigar cigar) {
		mainPanel.setVisible(false);
		mainPanel.removeAll();
		cigarDetailRatingPanel = new CigarDetailRatingPanel(cigar, ratingsDatabase);
		mainPanel.add(cigarDetailRatingPanel, BorderLayout.CENTER);
		cigarDetailRatingPanel.setLayout(null);
		mainPanel.repaint();
		mainPanel.setVisible(true);
	}
	
	public class CigarDatabaseSelectionListener implements ListSelectionListener {
	    JTable table;
	    CigarDatabaseSelectionListener(JTable table) {
	        this.table = table;
	    }
		public void valueChanged(ListSelectionEvent e) {
	        if (e.getValueIsAdjusting() == false) {
	        	Cigar newCigar = new Cigar("New Cigar");
	        	newCigar.setClearCigar();
	        	
	        	if (cigarDatabaseTable.getSelectedRow() == -1) {
	        		CigarDetailDatabasePanel.setCigarDatabaseButtonsVisible(true, true);

	        		buildCigarDatabaseDetails(newCigar);
	        		
	        	} else if (cigarDatabaseTable.getSelectedRowCount() > 1) {
	        		mainPanel.removeAll();
	        		mainPanel.repaint();
	        		CigarDetailDatabasePanel.setCigarDatabaseButtonsVisible(false, true);
	        		
	        	} else if (cigarDatabaseTable.getSelectedRowCount() == 1) {
	        		CigarDetailDatabasePanel.setCigarDatabaseButtonsVisible(true, true);
	        		cigar = cigarDatabase.getCigars().get(cigarDatabaseTable.convertRowIndexToModel(cigarDatabaseTable.getSelectedRow()));
	        		
	        		buildCigarDatabaseDetails(cigar);
	        	}
	        }
	    }
	}
	
	public class HumidorSelectionListener implements ListSelectionListener {
	    JTable table;
	    HumidorSelectionListener(JTable table) {
	        this.table = table;
	    }
		public void valueChanged(ListSelectionEvent e) {
	        if (e.getValueIsAdjusting() == false) {
	        	Cigar newCigar = new Cigar("New Cigar");
	        	newCigar.setClearCigar();
	 
	        	if (humidorTable.getSelectedRow() == -1) {
	        		CigarDetailHumidorPanel.setHumidorButtonsEnabled(true, true, true);
					
					buildHumidorDetails(newCigar);
	        		
	        	} else if (humidorTable.getSelectedRowCount() > 1) {
	        		mainPanel.removeAll();
	        		mainPanel.repaint();
	        		CigarDetailHumidorPanel.setHumidorButtonsEnabled(false, false, false);

	        	} else if (humidorTable.getSelectedRowCount() == 1) {
	        		CigarDetailHumidorPanel.setHumidorButtonsEnabled(true, true, true);
	        		cigar =	humidorDatabase.getCigars().get(humidorTable.convertRowIndexToModel(humidorTable.getSelectedRow()));
	        		buildHumidorDetails(cigar);
	        	}
	        }
	    }
	}
	
	

	
	public class RatingsSelectionListener implements ListSelectionListener {
	    JTable table;
	    RatingsSelectionListener(JTable table) {
	        this.table = table;
	    }
		public void valueChanged(ListSelectionEvent e) {
	        if (e.getValueIsAdjusting() == false) {
	        	Cigar newCigar = new Cigar("New Cigar");
	        	newCigar.setClearCigar();
	        	if (ratingsTable.getSelectedRow() == -1) {
	        		CigarDetailRatingPanel.editFields(false);
	        		buildRatingDetails(newCigar);
	        		
	        	} else if (ratingsTable.getSelectedRowCount() > 1) {
	        		CigarDetailRatingPanel.editFields(false);
	        		mainPanel.removeAll();
	        		mainPanel.repaint();
	        		
	        	} else if (ratingsTable.getSelectedRowCount() == 1) {
	        		CigarDetailRatingPanel.editFields(false);
	        		cigar = ratingsDatabase.getCigars().get(ratingsTable.convertRowIndexToModel(ratingsTable.getSelectedRow()));
	        		buildRatingDetails(cigar);
	        	}
	        }
	    }	
	}

	public static void refreshTables() {
		getDatabases();
		
		cigarDatabaseScrollPane.setViewportView(cigarDatabaseTable);
		DefaultTableModel cigarDatabaseModel = new DefaultTableModel();
		cigarDatabaseTable.setModel(cigarDatabaseModel);
		cigarDatabaseTable.setAutoCreateRowSorter(true);
		cigarDatabaseModel.setColumnIdentifiers(new String[] {"Name", "Size"});
		for (Cigar c1 : cigarDatabase.getCigars())
		{
			cigarDatabaseModel.addRow(new String [] {c1.getName(), c1.getSize()});
		}
		cigarDatabaseTable.getColumnModel().getColumn(0).setMinWidth(300);
		cigarDatabaseTable.getColumnModel().getColumn(1).setPreferredWidth(130);
		cigarDatabaseTable.getRowSorter().toggleSortOrder(0);
		
		humidorScrollPane.setViewportView(humidorTable);
		DefaultTableModel humidorDatabaseModel = new DefaultTableModel();
		humidorTable.setModel(humidorDatabaseModel);
		humidorTable.setAutoCreateRowSorter(true);
		humidorTable.setDefaultRenderer(Date.class, new DateRenderer());
		
		humidorDatabaseModel.setColumnIdentifiers(new String[] {"Name", "Qty.", "Date Added"});
		for (Cigar c1 : humidorDatabase.getCigars())
		{			
			humidorDatabaseModel.addRow(new Object [] {c1.getName(), Integer.toString(c1.getQuantity()), c1.getDateAddHumidor()});
		}
		humidorTable.getColumnModel().getColumn(0).setMinWidth(300);
		humidorTable.getColumnModel().getColumn(0).setPreferredWidth(400);
		humidorTable.getColumnModel().getColumn(1).setMinWidth(29);
		humidorTable.getColumnModel().getColumn(1).setPreferredWidth(29);
		humidorTable.getColumnModel().getColumn(2).setMinWidth(80);
		humidorTable.getColumnModel().getColumn(2).setPreferredWidth(80);
		humidorTable.getRowSorter().toggleSortOrder(0);
		
		ratingsScrollPane.setViewportView(ratingsTable);
		DefaultTableModel ratingsDatabaseModel = new DefaultTableModel();
		ratingsTable.setModel(ratingsDatabaseModel);
		ratingsTable.setAutoCreateRowSorter(true);
		ratingsTable.setDefaultRenderer(Date.class, new DateRenderer());
		ratingsDatabaseModel.setColumnIdentifiers(new String[] {"Name", "Size",	"My Rating", "Date Smoked"});
		for (Cigar c2 : ratingsDatabase.getCigars())
		{			
			ratingsDatabaseModel.addRow(new String [] {c2.getName(), c2.getSize(), Integer.toString(c2.getMyRating()), c2.getDateSmoked()});
		}
		ratingsTable.getColumnModel().getColumn(0).setMinWidth(100);
		ratingsTable.getColumnModel().getColumn(0).setPreferredWidth(250);
		ratingsTable.getColumnModel().getColumn(1).setMinWidth(90);
		ratingsTable.getColumnModel().getColumn(1).setPreferredWidth(100);
		ratingsTable.getColumnModel().getColumn(2).setMinWidth(20);
		ratingsTable.getColumnModel().getColumn(2).setPreferredWidth(80);
		ratingsTable.getColumnModel().getColumn(3).setMinWidth(80);
		ratingsTable.getColumnModel().getColumn(3).setPreferredWidth(80);
		ratingsTable.getRowSorter().toggleSortOrder(3);
	}
	
	public static void getDatabases() {
		// Create-Get Cigar Database		
				String cigarDatabaseFileName = Database.CIGAR_DB_FILENAME;
				cigarDatabase = new Database(cigarDatabaseFileName);
				File file1 = new File(cigarDatabaseFileName);
				boolean exists1 = file1.exists();
				if (exists1) {
					cigarDatabase = Database.readDatabase(cigarDatabaseFileName);
				}
				
		//Create-Get Humidor Database		
				String humidorDatabaseFileName = Database.HUMIDOR_DB_FILENAME;
				humidorDatabase = new Database(humidorDatabaseFileName);
				File file2 = new File(humidorDatabaseFileName);
				boolean exists2 = file2.exists();
				if (exists2) {
					humidorDatabase = Database.readDatabase(humidorDatabaseFileName);
				}
				
		//Create-Get Rating Database		
				String ratingsDatabaseFileName = Database.RATINGS_DB_FILENAME;
				ratingsDatabase = new Database(ratingsDatabaseFileName);
				File file3 = new File(ratingsDatabaseFileName);
				boolean exists3 = file3.exists();
				if (exists3) {
					ratingsDatabase = Database.readDatabase(ratingsDatabaseFileName);
				}			
	}
	
	/**
	 * Allows the window to be enabled
	 * @param enabled
	 */
	public static void setWindowEnabled(boolean enabled) {
		frmPersonalCigarAssistant.setEnabled(enabled);
		frmPersonalCigarAssistant.setFocusableWindowState(enabled);
	}
	
    private static class DateRenderer extends DefaultTableCellRenderer {

        /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");

        public DateRenderer() {
            super();
        }

        @Override
        public void setValue(Object value) {
            setText((value == null) ? "" : formatter.format(value));
        }
    }
}
